using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Camalon.Drawing
{
	/// <summary>
	/// Summary description for TextureFill.
	/// </summary>
	[ToolboxItem( true )]
	[ToolboxBitmap( typeof( ShapePen ), "Components.HatchFill.bmp")]
	public class HatchFill:ShapeFill, IMultiBrushFill
	{
		TransparentColor	_backColor;
		TransparentColor	_hatchColor;
		HatchStyle			_hatchStyle;
		HatchBrush			_brush;
		
		#region Constructors
		/// <summary>A hatch fill object</summary>
		public HatchFill():this(TransparentColor.Transparent)
		{
		}
		
		public HatchFill(TransparentColor backColor):base(FillType.Hatched)
		{
			_backColor =  backColor;
			_hatchColor = new TransparentColor( Color.Black );
			_hatchStyle = HatchStyle.Wave;
			RecreateBrush();
		}

		#endregion Constructors
		
		private void RecreateBrush()
		{
			Color backColor = _blend == null ?  _backColor.Color : Color.Transparent;

			_brush = new HatchBrush( _hatchStyle,_hatchColor.Color, backColor);
		}

		public Brush GetBrush()
		{
			return _brush.Clone() as Brush;
		}

		public Brush GetPenBrush( )
		{
			if( _blend == null) return _brush.Clone()as Brush;
			return  new HatchBrush( _hatchStyle,_hatchColor.Color, _backColor.Color);
		}

		#region Properties


		[Description("Color of spaces between the hatch lines drawn by this HatchFill object.")]
		[DefaultValue( typeof( TransparentColor),"Transparent")]
		[Category("Style")]
		public TransparentColor BackColor
		{
			get
			{
				return _backColor;
			}
			set
			{
				if( _backColor ==  value ) return;

				_backColor = value;
				RecreateBrush();
				InvokeInternalChanged();
			}
		}


		[Description("Color of hatch lines drawn by this HatchFill object.")]
		[DefaultValue( typeof( TransparentColor),"Black")]
		[Category("Style")]
		public TransparentColor HatchColor
		{
			get
			{
				return _hatchColor;
			}
			set
			{
				if( _hatchColor== value) return;

				if( _hatchColor.IsTransparent)
					new NotSupportedException("Transparent or empty color for hatches not supported");
				
				_hatchColor = value;
				RecreateBrush();
				InvokeInternalChanged();
			}
		}

		[Description("A HatchStyle enumeration that represents the pattern of this HatchFill.")]
		[DefaultValue(HatchStyle.Wave)]
        [Editor("Camalon.Drawing.Design.HatchStyleEditor, Camalon.Drawing.Design", typeof(System.Drawing.Design.UITypeEditor))]
        [Category("Style")]
		public HatchStyle HatchStyle
		{
			get{ return _hatchStyle;}
			set
			{ 
				if( _hatchStyle == value) return;

				_hatchStyle = value;
				RecreateBrush();
				InvokeInternalChanged();

			}

		}




		#region IMultiBrushFill Members
		ShapeFill _blend		= null;

		[DefaultValue(null)]
		[Category("Style")]
		public ShapeFill Blend
		{
			get{ return _blend;}
			set
			{ 
				if( value == _blend) return;

				if( value == this) 
					throw new InvalidOperationException("Blend cannot be set with containing HatchFill object!");

				bool needEvent = true;
				IMultiBrushFill mbFill = value as IMultiBrushFill;
				if( mbFill != null && mbFill.Blend == this) needEvent = false;

				if(needEvent && _blend != null) _blend.InternalChanged -= new EventHandler(OnInternalChanged);
				_blend = value;
				if(needEvent && _blend != null) _blend.InternalChanged += new EventHandler(OnInternalChanged);

				RecreateBrush();
				InvokeInternalChanged();
			}
		}

		bool _foregroundBlend;
		[DefaultValue(false)]
		[Category("Style")]
		public bool ForegroundBlend
		{
			get
			{
				return _foregroundBlend;
			}
			set
			{
				if( value == _foregroundBlend) return;

				_foregroundBlend = value;
				InvokeInternalChanged();
			}
		}

		#endregion

		#endregion Properties

	}	
}
